home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / interp_fast.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  1.8 KB  |  113 lines

  1. /*
  2.  * C code from the article
  3.  * "Faster Linear Interpolation"
  4.  * by Steven Eker, steve@cs.city.ac.uk
  5.  * in "Graphics Gems IV", Academic Press, 1994
  6.  */
  7.  
  8. #include <stdio.h>
  9. /*
  10.  *    Test routine:
  11.  *        Read in a, b, c
  12.  *        Compute interpolations using old and new routines
  13.  *        Check for differences
  14.  *        Print interpolation
  15.  */
  16. main()
  17. {
  18.   short o[1000], o2[1000];
  19.   int a, b, c, i;
  20.  
  21.   while(scanf("%d%d%d", &a, &b, &c) == 3){
  22.     dec_var(a, b, c, o);
  23.     linear(a, b, c, o2);
  24.     printf("\n");
  25.     for(i = 0; i <= a; i++){
  26.       if(o[i] != o2[i]){
  27.     printf("Error\n");
  28.     printf("i = %d, o[i] = %d, o2[i] = %d\n", i, o[i], o2[i]);
  29.     exit(1);
  30.       }
  31.       printf("%d ", o[i]);
  32.     }
  33.     printf("\n\n");
  34.   }
  35. }
  36.  
  37. /*
  38.  *    Decision variable method
  39.  */
  40. dec_var(a, b, c, o)
  41. int a, b, c;
  42. short *o;
  43. {
  44.   int i1 = b / a, e1 = 2 * (b % a);
  45.   int i2 = i1 + 1, e2 = e1 - 2 * a;
  46.   int t = c, r = e1 - a;
  47.  
  48.   do{
  49.     *o++ = t;
  50.     if(r < 0){
  51.       t += i1; r += e1;
  52.     }
  53.     else{
  54.       t += i2; r += e2;
  55.     }
  56.   }while(--a >= 0);
  57. }
  58.  
  59. /*
  60.  *    Fast decision variable method
  61.  */
  62. linear(a, b, c, o)
  63. int a, b, c;
  64. short *o;
  65. {
  66.   int t, i1, i2;
  67.  
  68.   if(a > b){
  69.     t = 0; i1 = b;
  70.   }
  71.   else{
  72.     t = b / a; i1 = b % a;
  73.   }
  74.   i1 = (i1 << 16) + t;
  75.   i2 = i1 - (a << 16) + 1;
  76.   t = c - t - (((a + 1) >> 1) << 16);
  77.   switch(a & 3){
  78.   do{
  79.   case 3:
  80.     if((t += i1) >= 0) goto pos1;
  81. neg1:
  82.     *o++ = t;
  83.   case 2:
  84.     if((t += i1) >= 0) goto pos2;
  85. neg2:
  86.     *o++ = t;
  87.   case 1:
  88.     if((t += i1) >= 0) goto pos3;
  89. neg3:
  90.     *o++ = t;
  91.   case 0:
  92.     if((t += i1) >= 0) goto pos4;
  93. neg4:
  94.     *o++ = t;
  95.   }while((a -= 4) >= 0);
  96.   }
  97.   return;
  98.   do{
  99.     if((t += i2) < 0) goto neg1;
  100. pos1:
  101.     *o++ = t;
  102.     if((t += i2) < 0) goto neg2;
  103. pos2:
  104.     *o++ = t;
  105.     if((t += i2) < 0) goto neg3;
  106. pos3:
  107.     *o++ = t;
  108.     if((t += i2) < 0) goto neg4;
  109. pos4:
  110.     *o++ = t;
  111.   }while((a -= 4) >= 0);
  112. }
  113.